Skip to content

Conversation

@RulaKhaled
Copy link
Member

@RulaKhaled RulaKhaled commented Nov 6, 2025

This PR adds official support for instrumenting LangGraph StateGraph operations in Node with Sentry tracing, following OpenTelemetry semantic conventions for Generative AI.

Currently supported:

Node.js - Both agent creation and invocation are instrumented in this PR
ESM and CJS - Both module systems are supported

The langGraphIntegration() accepts the following options:

// The integration respects your sendDefaultPii client option
interface LangGraphOptions {
  recordInputs?: boolean;   // Whether to record input messages
  recordOutputs?: boolean;  // Whether to record response text and tool calls
}

e.g

Sentry.init({
  dsn: '__DSN__',
  sendDefaultPii: false, // Even with PII disabled globally
  integrations: [
    Sentry.langGraphIntegration({
      recordInputs: true,    // Force recording input messages
      recordOutputs: true,   // Force recording response text
    }),
  ],
});

Operations traced:

  • gen_ai.create_agent - Spans created when StateGraph.compile() is called
  • gen_ai.invoke_agent - Spans created when CompiledGraph.invoke() is called

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

size-limit report 📦

Path Size % Change Change
@sentry/browser 24.64 kB - -
@sentry/browser - with treeshaking flags 23.14 kB - -
@sentry/browser (incl. Tracing) 41.29 kB - -
@sentry/browser (incl. Tracing, Profiling) 45.58 kB - -
@sentry/browser (incl. Tracing, Replay) 79.75 kB - -
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 69.45 kB - -
@sentry/browser (incl. Tracing, Replay with Canvas) 84.44 kB - -
@sentry/browser (incl. Tracing, Replay, Feedback) 96.66 kB - -
@sentry/browser (incl. Feedback) 41.29 kB - -
@sentry/browser (incl. sendFeedback) 29.3 kB - -
@sentry/browser (incl. FeedbackAsync) 34.22 kB - -
@sentry/react 26.33 kB - -
@sentry/react (incl. Tracing) 43.23 kB - -
@sentry/vue 29.12 kB - -
@sentry/vue (incl. Tracing) 43.09 kB - -
@sentry/svelte 24.66 kB - -
CDN Bundle 26.96 kB - -
CDN Bundle (incl. Tracing) 41.84 kB - -
CDN Bundle (incl. Tracing, Replay) 78.39 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) 83.85 kB - -
CDN Bundle - uncompressed 78.94 kB - -
CDN Bundle (incl. Tracing) - uncompressed 124.09 kB - -
CDN Bundle (incl. Tracing, Replay) - uncompressed 240.12 kB - -
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 252.88 kB - -
@sentry/nextjs (client) 45.38 kB - -
@sentry/sveltekit (client) 41.68 kB - -
@sentry/node-core 50.91 kB - -
@sentry/node 159.09 kB +0.63% +984 B 🔺
@sentry/node - without tracing 92.78 kB +0.01% +1 B 🔺
@sentry/aws-serverless 106.53 kB - -

View base workflow run

@github-actions
Copy link
Contributor

github-actions bot commented Nov 6, 2025

node-overhead report 🧳

Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.

Scenario Requests/s % of Baseline Prev. Requests/s Change %
GET Baseline 9,221 - 8,400 +10%
GET With Sentry 1,352 15% 1,305 +4%
GET With Sentry (error only) 6,017 65% 5,789 +4%
POST Baseline 1,192 - 1,129 +6%
POST With Sentry 534 45% 465 +15%
POST With Sentry (error only) 1,079 91% 1,040 +4%
MYSQL Baseline 3,386 - 3,218 +5%
MYSQL With Sentry 520 15% 410 +27%
MYSQL With Sentry (error only) 2,781 82% 2,581 +8%

View base workflow run

@RulaKhaled RulaKhaled force-pushed the instrument-langgrapgh-in-node branch from e45920a to 932f908 Compare November 17, 2025 09:11
Copy link
Member

@nicohrubec nicohrubec left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

export function extractTokenUsageFromMetadata(span: Span, message: LangChainMessage): void {
const msg = message as Record<string, unknown>;

// Extract from usage_metadata (newer format)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

l: Sure, though I think it would be good then to put that detail in the comment or maybe a link where that is explained (if you have one) so that it is more clear

@RulaKhaled RulaKhaled merged commit f9e714f into develop Nov 17, 2025
195 checks passed
@RulaKhaled RulaKhaled deleted the instrument-langgrapgh-in-node branch November 17, 2025 11:23
RulaKhaled added a commit that referenced this pull request Nov 19, 2025
#18112)

This PR adds manual instrumentation support for LangGraph StateGraph
operations in Cloudflare Workers and Vercel Edge environments.

```
import * as Sentry from '@sentry/cloudflare'; // or '@sentry/vercel-edge'
import { StateGraph, START, END, MessagesAnnotation } from '@langchain/langgraph';

// Create and instrument the graph
const graph = new StateGraph(MessagesAnnotation)
  .addNode('agent', agentFn)
  .addEdge(START, 'agent')
  .addEdge('agent', END);

Sentry.instrumentLangGraph(graph, {
  recordInputs: true,
  recordOutputs: true,
});

const compiled = graph.compile({ name: 'weather_assistant' });

await compiled.invoke({
  messages: [{ role: 'user', content: 'What is the weather in SF?' }],
});
```

- [x] This PR depends on #18114
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants